<?php 
/**
 *  Por Federico Pisarello - SisDev Software (c) 2012 - Buenos Aires, Argentina.
 * 	fpisarello@sisdevsoft.com
 */

require("includes/funciones.inc.php");

session_start();
if(!isset($_SESSION['nombreUsuario']))
	exit();

function searchABMClientes()
{
	require("includes/constants.php");
	require("includes/datatables_db/Datatables.php");
	
	$datatables = new Datatables();
	
	$datatables
			->select('idCliente, codCliente, razonSocial, direccion, localidad, provincia, telefono, tipos_iva.descripcion, cuit')
			->from('clientes')
			->join('tipos_iva','clientes.idTipoIva = tipos_iva.idTipoIva', 'inner')
			->where("clientes.estado <> '".$stateErase."'");

	return $datatables->generate();	
}

function addOrEdit()
{
	require("includes/constants.php");

	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;
	
	$DB->StartTrans(); // start transaction
	
	if(!isset($_REQUEST["idCliente"])){
			
		$strSQL = "INSERT INTO clientes ( ";
		$strSQL .= "	razonSocial, ";
		$strSQL .= "	idTipoIva, ";
		$strSQL .= "	cuit, ";
		$strSQL .= "	direccion, ";
		$strSQL .= "	localidad, ";
		$strSQL .= "	provincia, ";
		$strSQL .= "	codPostal, ";
		$strSQL .= "	telefono, ";
		$strSQL .= "	eMail, ";
		$strSQL .= "	estado ";
		$strSQL .= "	) VALUES ( ";
		$strSQL .= "	'".mb_strtoupper(str_replace("'", "''", $_REQUEST["razonSocial"]), "UTF-8")."', ";
		$strSQL .= "	'".$_REQUEST["cmbIVA"]."', ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["cuit"], "UTF-8")."', ";
		$strSQL .= "	'".mb_strtoupper(str_replace("'", "''", $_REQUEST["direccion"]), "UTF-8")."', ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["localidad"], "UTF-8")."', ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["provincia"], "UTF-8")."', ";
		$strSQL .= "	'".mb_strtoupper($_REQUEST["codPostal"], "UTF-8")."', ";
		$strSQL .= "	'".mb_strtoupper(str_replace("'", "''", $_REQUEST["telefono"]), "UTF-8")."', ";
		$strSQL .= "	'".$_REQUEST["eMail"]."', ";
		$strSQL .= "	'".$stateAdd."' ";
		$strSQL .= "	)";
		
		$DB->Execute($strSQL);
	}
	else{
		$strSQL = "UPDATE clientes SET ";
		$strSQL .= "	razonSocial = '".mb_strtoupper(str_replace("'", "''", $_REQUEST["razonSocial"]), "UTF-8")."', ";
		$strSQL .= "	cuit = '".mb_strtoupper($_REQUEST["cuit"], "UTF-8")."', ";
		$strSQL .= "	direccion = '".mb_strtoupper(str_replace("'", "''", $_REQUEST["direccion"]), "UTF-8")."', ";
		$strSQL .= "	localidad = '".mb_strtoupper($_REQUEST["localidad"], "UTF-8")."', ";
		$strSQL .= "	provincia = '".mb_strtoupper($_REQUEST["provincia"], "UTF-8")."', ";
		$strSQL .= "	codPostal = '".mb_strtoupper($_REQUEST["codPostal"], "UTF-8")."', ";
		$strSQL .= "	telefono = '".mb_strtoupper(str_replace("'", "''", $_REQUEST["telefono"]), "UTF-8")."', ";
		$strSQL .= "	eMail = '".$_REQUEST["eMail"]."', ";
		$strSQL .= "	estado = '".$stateModify."' ";
		$strSQL .= " WHERE idCliente = ". $_REQUEST["idCliente"];
		
		$DB->Execute($strSQL);
	}
	
	if (!$DB->CompleteTrans())
	{
		$arrJSON->status = "ERROR";
		$arrJSON->msg = "Error al Grabar el Cliente";
	}
	else
	{
		$arrJSON->status = "OK";
		$arrJSON->msg = "El Cliente fue Guardado con Exito";
	}	
	return json_encode($arrJSON);
}

function del()
{
	require("includes/constants.php");
	
	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;
	
	$DB->StartTrans(); // start transaction
	$DB->Execute("UPDATE clientes SET estado = '$stateErase' WHERE idCliente = " . $_REQUEST["idCliente"]); // execute query

	if (!$DB->CompleteTrans())
	{
		$arrJSON->status = "ERROR";
		$arrJSON->msg = "Error al Eliminar el Cliente";
	}
	else
	{
		$arrJSON->status = "OK";
		$arrJSON->msg = "El Cliente se Elimino Correctamente";
	}	
	return json_encode($arrJSON);
}

function editCliente()
{
	require("includes/constants.php");
	
	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;
	
	$rsClientes = $DB->Execute("SELECT * FROM clientes WHERE estado <> '$stateErase' AND idCliente = " . $_REQUEST["idCliente"]); // execute query
						
	if(!$rsClientes->EOF){
		$joCliente->data['razonSocial'] = $rsClientes->fields("razonSocial");
		$joCliente->data['idTipoIva'] = $rsClientes->fields("idTipoIva");
		$joCliente->data['cuit'] = $rsClientes->fields("cuit");
		$joCliente->data['direccion'] = $rsClientes->fields("direccion");
		$joCliente->data['localidad'] = $rsClientes->fields("localidad");
		$joCliente->data['provincia'] = $rsClientes->fields("provincia");
		$joCliente->data['codPostal'] = $rsClientes->fields("codPostal");
		$joCliente->data['telefono'] = $rsClientes->fields("telefono");
		$joCliente->data['eMail'] = $rsClientes->fields("eMail");
		
		return json_encode($joCliente->data);
	}
	else
		return json_encode(array());
}

function getTiposIva()
{
	$DB = NewADOConnection('mysqlt');
	$DB->Connect();
	$DB->Execute("SET NAMES utf8;");
	//$DB->debug=true;
	
	$rsTiposIva = $DB->Execute("SELECT * FROM tipos_iva");
	
	$i=0;
	while(!$rsTiposIva->EOF){
		$arrTiposIva->data[$i]['idTipoIva'] = $rsTiposIva->fields('idTipoIva');
		$arrTiposIva->data[$i++]['descripcion'] = $rsTiposIva->fields('descripcion');
		$rsTiposIva->MoveNext();
	}
	
	return json_encode($arrTiposIva->data);
}

function migrarDatos()
{
	require("includes/constants.php");
	require("interfase/clientes.php");
	
	$arrClientes = array();
	$cli = fopen("interfase/CLIENTES.DAT", "r"); 
 	while(!feof($cli)) {
    	$strCliente = fread($cli,160);
    	$cliente = new Clientes($strCliente);
    	array_push($arrClientes, $cliente);	 
 	}
 	fclose($cli);
 	
 	$DB = NewADOConnection('mysqlt');
 	$DB->Connect();
 	$DB->Execute("SET NAMES utf8;");
 	//$DB->debug=true;
 	
 	$DB->StartTrans(); // start transaction
 	
 	foreach ($arrClientes as $cliente) {
 		if(!empty($cliente->nro)) {
	 		$strSQL = "INSERT INTO clientes ( ";
	 		$strSQL .= "	codCliente, ";
	 		$strSQL .= "	razonSocial, ";
	 		$strSQL .= "	idTipoIva, ";
	 		$strSQL .= "	cuit, ";
	 		$strSQL .= "	direccion, ";
	 		$strSQL .= "	localidad, ";
	 		$strSQL .= "	provincia, ";
	 		$strSQL .= "	codPostal, ";
	 		$strSQL .= "	telefono, ";
	 		$strSQL .= "	eMail, ";
	 		$strSQL .= "	estado ";
	 		$strSQL .= "	) VALUES ( ";
	 		$strSQL .= "	'".mb_strtoupper(str_replace("'", "''", $cliente->nro), "UTF-8")."', ";
	 		$strSQL .= "	'".mb_strtoupper(str_replace("'", "''", $cliente->razonSocial), "UTF-8")."', ";
	 		$strSQL .= "	'"."1"."', ";
	 		$strSQL .= "	'".mb_strtoupper($cliente->CUIT, "UTF-8")."', ";
	 		$strSQL .= "	'".mb_strtoupper(str_replace("'", "''", $cliente->direccion), "UTF-8")."', ";
	 		$strSQL .= "	'".mb_strtoupper($cliente->localidad, "UTF-8")."', ";
	 		$strSQL .= "	'".mb_strtoupper($cliente->provincia, "UTF-8")."', ";
	 		$strSQL .= "	'".mb_strtoupper($cliente->codPostal, "UTF-8")."', ";
	 		$strSQL .= "	'".mb_strtoupper(str_replace("'", "''", $cliente->telefono), "UTF-8")."', ";
	 		$strSQL .= "	'', ";
	 		$strSQL .= "	'".$stateAdd."' ";
	 		$strSQL .= "	)";
	 		
	 		$DB->Execute($strSQL);
 		}
 	}
 	
 	
 	if (!$DB->CompleteTrans())
 	{
 		$arrJSON->status = "ERROR";
 		$arrJSON->msg = "Error en la Interfase de Clientes";
 	}
 	else
 	{
 		$arrJSON->status = "OK";
 		$arrJSON->msg = "La Interfase se ejecuto con Exito";
 	}
 	return json_encode($arrJSON);

}

switch($_REQUEST['accion'])
{
	case "EDIT":
		echo editCliente();
		break;
	case "DELETE":
		echo del();
		break;
	case "search":
		echo searchABMClientes();
		break;
	case "addOrEdit":
		echo addOrEdit();
		break;
	case "getTiposIva":
		echo getTiposIva();
		break;
		
	//Migrar datos sistema Alberto
	case "migrarDatos":
		echo migrarDatos();
		break;
}
?>
